package com.surelution.projects.crm

class MarketGasSaleController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [marketGasSaleInstanceList: MarketGasSale.list(params), marketGasSaleInstanceTotal: MarketGasSale.count()]
    }

    def create = {
        def marketGasSaleInstance = new MarketGasSale()
        marketGasSaleInstance.properties = params
        return [marketGasSaleInstance: marketGasSaleInstance]
    }

    def save = {
        def marketGasSaleInstance = new MarketGasSale(params)
        if (marketGasSaleInstance.save(flush: true)) {
			def mId = marketGasSaleInstance.market.id
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'marketGasSale.label', default: 'MarketGasSale'), marketGasSaleInstance.id])}"
            redirect(action: "show", id: mId,controller:"market")
        }
        else {
            render(view: "create", model: [marketGasSaleInstance: marketGasSaleInstance])
        }
    }

    def show = {
        def marketGasSaleInstance = MarketGasSale.get(params.id)
        if (!marketGasSaleInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'marketGasSale.label', default: 'MarketGasSale'), params.id])}"
            redirect(action: "list")
        }
        else {
            [marketGasSaleInstance: marketGasSaleInstance]
        }
    }

    def edit = {
        def marketGasSaleInstance = MarketGasSale.get(params.id)
        if (!marketGasSaleInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'marketGasSale.label', default: 'MarketGasSale'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [marketGasSaleInstance: marketGasSaleInstance]
        }
    }

    def update = {
        def marketGasSaleInstance = MarketGasSale.get(params.id)
		def mId = marketGasSaleInstance.market.id
        if (marketGasSaleInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (marketGasSaleInstance.version > version) {
                    
                    marketGasSaleInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'marketGasSale.label', default: 'MarketGasSale')] as Object[], "Another user has updated this MarketGasSale while you were editing")
                    render(view: "edit", model: [marketGasSaleInstance: marketGasSaleInstance])
                    return
                }
            }
            marketGasSaleInstance.properties = params
            if (!marketGasSaleInstance.hasErrors() && marketGasSaleInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'marketGasSale.label', default: 'MarketGasSale'), marketGasSaleInstance.id])}"
				redirect(action: "show", id: mId,controller:"market")
            }
            else {
                render(view: "edit", model: [marketGasSaleInstance: marketGasSaleInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'marketGasSale.label', default: 'MarketGasSale'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def marketGasSaleInstance = MarketGasSale.get(params.id)
		def mId = marketGasSaleInstance.market.id
        if (marketGasSaleInstance) {
            try {
                marketGasSaleInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'marketGasSale.label', default: 'MarketGasSale'), params.id])}"
                redirect(action: "show",controller:"market",id:mId)
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'marketGasSale.label', default: 'MarketGasSale'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'marketGasSale.label', default: 'MarketGasSale'), params.id])}"
            redirect(action: "list")
        }
    }
}
